﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models
{
    public class ConfigurationModel
    {
        [DisplayName("1")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Frequency1 { get; set; }
        [DisplayName("2")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Frequency2 { get; set; }
        [DisplayName("3")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Frequency3 { get; set; }
        [DisplayName("4")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Frequency4 { get; set; }
        [DisplayName("5")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Frequency5 { get; set; }

        [DisplayName("1")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Exposed1 { get; set; }
        [DisplayName("2")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Exposed2 { get; set; }
        [DisplayName("3")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Exposed3 { get; set; }

        [DisplayName("1")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Controls1 { get; set; }
        [DisplayName("2")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Controls2 { get; set; }
        [DisplayName("3")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Controls3 { get; set; }

        [DisplayName("1")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Severity1 { get; set; }
        [DisplayName("2")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Severity2 { get; set; }
        [DisplayName("3")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Severity3 { get; set; }
        [DisplayName("4")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Severity4 { get; set; }
        [DisplayName("5")]
        [Required(ErrorMessage = "Este parametro es requerido")]
        [StringLength(100, ErrorMessage = "El parametro debe tener maximo 100 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El parametro solo debe tener letras y numeros")]
        public string Severity5 { get; set; }

        riskanalysisEntities entities = new riskanalysisEntities();

        public ConfigurationModel()
        {
            FillFrequences();
            FillExposed();
            FillControls();
            FillSeveritys();
        }

        private void FillFrequences()
        {
            var query = from f in entities.frequences
                        where f.frequenceId == 1
                        select f;
            Frequency1 = query.FirstOrDefault().name;
            query = from f in entities.frequences
                    where f.frequenceId == 2
                    select f;
            Frequency2 = query.FirstOrDefault().name;
            query = from f in entities.frequences
                    where f.frequenceId == 3
                    select f;
            Frequency3 = query.FirstOrDefault().name;
            query = from f in entities.frequences
                    where f.frequenceId == 4
                    select f;
            Frequency4 = query.FirstOrDefault().name;
            query = from f in entities.frequences
                    where f.frequenceId == 5
                    select f;
            Frequency5 = query.FirstOrDefault().name;
        }

        private void FillSeveritys()
        {
            var query = from s in entities.severities
                        where s.severityId == 1
                        select s;
            Severity1 = query.FirstOrDefault().name;
            query = from s in entities.severities
                    where s.severityId == 2
                    select s;
            Severity2 = query.FirstOrDefault().name;
            query = from s in entities.severities
                    where s.severityId == 3
                    select s;
            Severity3 = query.FirstOrDefault().name;
            query = from s in entities.severities
                    where s.severityId == 4
                    select s;
            Severity4 = query.FirstOrDefault().name;
            query = from s in entities.severities
                    where s.severityId == 5
                    select s;
            Severity5 = query.FirstOrDefault().name;
        }

        private void FillExposed()
        {
            var query = from e in entities.affecteds
                        where e.affectedId == 1
                        select e;
            Exposed1 = query.FirstOrDefault().name;
            query = from e in entities.affecteds
                    where e.affectedId == 2
                    select e;
            Exposed2 = query.FirstOrDefault().name;
            query = from e in entities.affecteds
                    where e.affectedId == 3
                    select e;
            Exposed3 = query.FirstOrDefault().name;
        }

        private void FillControls()
        {
            var query = from c in entities.controlexistances
                        where c.controlExistanceId == 1
                        select c;
            Controls1 = query.FirstOrDefault().name;

            query = from c in entities.controlexistances
                    where c.controlExistanceId == 2
                    select c;
            Controls2 = query.FirstOrDefault().name;

            query = from c in entities.controlexistances
                    where c.controlExistanceId == 3
                    select c;
            Controls3 = query.FirstOrDefault().name;
        }

        public void Save()
        {
            SaveFrequences(1, Frequency1);
            SaveFrequences(2, Frequency2);
            SaveFrequences(3, Frequency3);
            SaveFrequences(4, Frequency4);
            SaveFrequences(5, Frequency5);
            SaveSeveritys(1, Severity1);
            SaveSeveritys(2, Severity2);
            SaveSeveritys(3, Severity3);
            SaveSeveritys(4, Severity4);
            SaveSeveritys(5, Severity5);
            SaveExposed(1, Exposed1);
            SaveExposed(2, Exposed2);
            SaveExposed(3, Exposed3);
            SaveControls(1, Controls1);
            SaveControls(2, Controls2);
            SaveControls(3, Controls3);
        }

        private void SaveFrequences(int id, string value)
        {
            var query = from f in entities.frequences
                        where f.frequenceId == id
                        select f;
            query.FirstOrDefault().name = value;
            entities.SaveChanges();
        }

        private void SaveSeveritys(int id, string value)
        {
            var query = from s in entities.severities
                        where s.severityId == id
                        select s;
            query.FirstOrDefault().name = value;
            entities.SaveChanges();
        }

        private void SaveExposed(int id, string value)
        {
            var query = from a in entities.affecteds
                        where a.affectedId == id
                        select a;
            query.FirstOrDefault().name = value;
            entities.SaveChanges();
        }

        private void SaveControls(int id, string value)
        {
            var query = from c in entities.controlexistances
                        where c.controlExistanceId == id
                        select c;
            query.FirstOrDefault().name = value;
            entities.SaveChanges();
        }
    }
}